/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.serverpackets;

import java.io.IOException;
import java.util.List;

import l1j.server.Config;
import l1j.server.server.Opcodes;
import l1j.server.server.datatables.ShopTable;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1TaxCalculator;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.shop.L1Shop;
import l1j.server.server.templates.L1Item;
import l1j.server.server.templates.L1ShopItem;

public class S_ShopSellList extends ServerBasePacket {

	public S_ShopSellList(int objId) {
		writeC(Opcodes.S_OPCODE_SHOWSHOPBUYLIST);
		writeD(objId);

		L1Object npcObj = L1World.getInstance().findObject(objId);
		if (!(npcObj instanceof L1NpcInstance)) {
			writeH(0);
			return;
		}
		int npcId = ((L1NpcInstance) npcObj).getNpcTemplate().get_npcId();

		L1TaxCalculator calc = new L1TaxCalculator(npcId);
		L1Shop shop = ShopTable.getInstance().get(npcId);
		List<L1ShopItem> shopItems = shop.getSellingItems();

		writeH(shopItems.size());
		for (int i = 0; i < shopItems.size(); i++) {
			L1ShopItem shopItem = shopItems.get(i);
			int price = calc.layTax((int) (shopItem.getPrice() * Config.RATE_SHOP_SELLING_PRICE));

			writeD(i);
			writeH(shopItem.getItem().getGfxId());
			writeD(price);
			writeItemData(npcId, shopItem.getItem());
		}
	}

	private void writeItemData(int npcid, L1Item item) {
		switch (item.getItemId()) {
		case 40738:
			writeS(item.getName() + " (2)");
			writeC(0x08);
			writeC(0x01);
			writeC(item.getDmgSmall());
			writeC(item.getDmgLarge());
			writeC(item.getMaterial());
			writeD(item.getWeight());
			break;
		case 40743:
			switch (npcid) {
			case 70026:
				writeS(item.getName() + " (11)");
				writeC(0x08);
				writeC(0x01);
				writeC(item.getDmgSmall());
				writeC(item.getDmgLarge());
				writeC(item.getMaterial());
				writeD(0);
				break;
			default:
				writeS(item.getName() + " (10)");
				writeC(0x08);
				writeC(0x01);
				writeC(item.getDmgSmall());
				writeC(item.getDmgLarge());
				writeC(item.getMaterial());
				writeD(0);
				break;
			}
			break;
		case 40744:
			switch (npcid) {
			case 70026:
				writeS(item.getName() + " (6)");
				writeC(0x08);
				writeC(0x01);
				writeC(item.getDmgSmall());
				writeC(item.getDmgLarge());
				writeC(item.getMaterial());
				writeD(0);
				break;
			default:
				writeS(item.getName() + " (5)");
				writeC(0x08);
				writeC(0x01);
				writeC(item.getDmgSmall());
				writeC(item.getDmgLarge());
				writeC(item.getMaterial());
				writeD(0);
				break;
			}
			break;
		default:
			if (item.getType2() == 1) { // 武器
				writeS(item.getName());
				// writeC(0x08);
				writeC(28);
				writeC(0x01);
				writeC(item.getDmgSmall());
				writeC(item.getDmgLarge());
				writeC(item.getMaterial());
				writeD(item.getWeight() / 1000);

				int totalByte = 0;
				if (item.getType() == 3 // tohandsword
						|| item.getType() == 4 // bow
						|| item.getType() == 5 // spear
						|| item.getType() == 11 // claw
						|| item.getType() == 12) { // edoryu
					writeC(4);
					totalByte += 1;
				}
				if (item.getHitModifier() != 0) {
					writeC(5);
					writeC(item.getHitModifier());
					totalByte += 2;
				}
				if (item.getDmgModifier() != 0) {
					writeC(6);
					writeC(item.getDmgModifier());
					totalByte += 2;
				}
				boolean use_all_class = item.isUseDarkelf() && item.isUseElf()
						&& item.isUseKnight() && item.isUseRoyal()
						&& item.isUseMage();
				if (!use_all_class) {
					int bit = 0;
					bit |= item.isUseRoyal() ? 1 : 0;
					bit |= item.isUseKnight() ? 2 : 0;
					bit |= item.isUseElf() ? 4 : 0;
					bit |= item.isUseMage() ? 8 : 0;
					bit |= item.isUseDarkelf() ? 16 : 0;
					writeC(7);
					writeC(bit);
					totalByte += 2;
				}
				if (item.getItemId() == 126 || item.getItemId() == 127) { // マナスタッフ、鋼鉄のマナスタッフ
					writeC(16);
					totalByte += 1;
				}
				if (item.isHasteItem()) {
					writeC(18);
					totalByte += 1;
				}
				if (item.get_addstr() != 0) {
					writeC(8);
					writeC(item.get_addstr());
					totalByte += 2;
				}
				if (item.get_adddex() != 0) {
					writeC(9);
					writeC(item.get_adddex());
					totalByte += 2;
				}
				if (item.get_addcon() != 0) {
					writeC(10);
					writeC(item.get_addcon());
					totalByte += 2;
				}
				if (item.get_addwis() != 0) {
					writeC(11);
					writeC(item.get_addwis());
					totalByte += 2;
				}
				if (item.get_addint() != 0) {
					writeC(12);
					writeC(item.get_addint());
					totalByte += 2;
				}
				if (item.get_addcha() != 0) {
					if (totalByte < 19) {
						writeC(13);
						writeC(item.get_addcha());
						totalByte += 2;
					}
				}
				if (item.get_addhp() != 0) {
					if (totalByte < 18) {
						writeC(14);
						writeH(item.get_addhp());
						totalByte += 3;
					}
				}
				if (item.get_addmp() != 0) {
					if (totalByte < 19) {
						writeC(32);
						writeC(item.get_addmp());
						totalByte += 2;
					}
				}
				if (item.get_addsp() != 0) {
					if (totalByte < 19) {
						writeC(17);
						writeC(item.get_addsp());
						totalByte += 2;
					}
				}
				// 追加効果情報は1アイテム20byteまで
				for (; totalByte < 20; totalByte++) {
					writeC(0);
				}
			} else if (item.getType2() == 2) { // 防具
				writeS(item.getName());
				writeC(28);
				writeC(0x13);
				writeC(-1 * item.get_ac());
				writeC(item.getMaterial());
				writeD(item.getWeight() / 1000);

				int totalByte = 0;
				boolean use_all_class = item.isUseDarkelf() && item.isUseElf()
						&& item.isUseKnight() && item.isUseRoyal()
						&& item.isUseMage();
				if (!use_all_class) {
					int bit = 0;
					bit |= item.isUseRoyal() ? 1 : 0;
					bit |= item.isUseKnight() ? 2 : 0;
					bit |= item.isUseElf() ? 4 : 0;
					bit |= item.isUseMage() ? 8 : 0;
					bit |= item.isUseDarkelf() ? 16 : 0;
					writeC(7);
					writeC(bit);
					totalByte += 2;
				}
				if (item.isHasteItem()) {
					writeC(18);
					totalByte += 1;
				}
				if (item.get_addstr() != 0) {
					writeC(8);
					writeC(item.get_addstr());
					totalByte += 2;
				}
				if (item.get_adddex() != 0) {
					writeC(9);
					writeC(item.get_adddex());
					totalByte += 2;
				}
				if (item.get_addcon() != 0) {
					writeC(10);
					writeC(item.get_addcon());
					totalByte += 2;
				}
				if (item.get_addwis() != 0) {
					writeC(11);
					writeC(item.get_addwis());
					totalByte += 2;
				}
				if (item.get_addint() != 0) {
					writeC(12);
					writeC(item.get_addint());
					totalByte += 2;
				}
				if (item.get_addcha() != 0) {
					writeC(13);
					writeC(item.get_addcha());
					totalByte += 2;
				}
				if (item.get_addhp() != 0) {
					writeC(14);
					writeH(item.get_addhp());
					totalByte += 3;
				}
				if (item.get_addmp() != 0) {
					writeC(32);
					writeC(item.get_addmp());
					totalByte += 2;
				}
				if (item.get_mdef() != 0) {
					if (totalByte < 20) {
						writeC(15);
						writeC(item.get_mdef());
						totalByte += 2;
					}
				}
				if (item.get_addsp() != 0) {
					if (totalByte < 20) {
						writeC(17);
						writeC(item.get_addsp());
						totalByte += 2;
					}
				}

				// 追加効果情報は1アイテム21byteまで
				for (; totalByte < 21; totalByte++) {
					writeC(0);
				}
			} else {
				if (item.getBless() == 0) {
					writeS("$227 " + item.getName());
				} else if (item.getBless() == 2) {
					writeS("$228 " + item.getName());
				} else {
					writeS(item.getName());
				}
				writeC(0x06);
				writeC(0x17);
				writeC(item.getMaterial());
				writeH(item.getWeight() / 1000);
				writeH(0x00);
			}
			break;
		}
	}

	@Override
	public byte[] getContent() throws IOException {
		return getBytes();
	}
}
